﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dtos;
using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class SigninBll:ISigninBll
    {
        public ISigninDal _S;
        public SigninBll(ISigninDal S) 
        {
           _S = S;
        }
        public async Task<int> AddSignInAsync(Guid studentId, int signStatus, int restTime)
        {
            return await _S.AddAsync(new Model.SignIn
            {
                studentId = studentId,
                signStatus = signStatus,
                restTime = restTime
            });
        }

        public async Task<int> DeleteSignInAsync(Guid id)
        {
            var data = await _S.GetIdAll(id);
            return data != null ? await _S.DeleteAsync(data) : 0;
        }

        public async Task<int> EditSignInAsync(Guid id, Guid studentId, int signStatus, int restTime)
        {
            var data = await _S.GetIdAll(id);
            if (data == null)
                return 0;
            data.signStatus = signStatus;
            data.restTime = restTime;
            data.studentId = studentId;
            data.UpdateTime = DateTime.Now;
            return await _S.DeleteAsync(data);
        }

        public async Task<List<SignInDtos>> getAllSignIn()
        {
            var data = await _S.GetAll();
            return data.Select(d => new SignInDtos
            {
                Id = d.Id,
                studentId = d.studentId,
                signStatus = d.signStatus,
                restTime = d.restTime,
                UpdateTime = DateTime.Now
            }).OrderByDescending(d => d.UpdateTime).ToList();
        }

        public async Task<SignInDtos> GetSignInIdAsync(Guid id)
        {
            throw new NotImplementedException();
        }
    }
}
